package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    CheckItemService checkItemService;

    @Override
    public boolean saveCheckGroup(CheckGroupDTO checkGroupDTO) {
        //看下前端要有没有传过来id值
        Integer checkGroupDTOId = checkGroupDTO.getId();
        //如果前端传过来id值，就先把中间表通过id进行删除
        if (StringUtils.isNotBlank(String.valueOf(checkGroupDTOId))){
            baseMapper.deleteCheckItemIdsByCheckGroupId(checkGroupDTOId);
        }
        boolean flag = this.saveOrUpdate(checkGroupDTO);
        Integer checkGroupId = checkGroupDTO.getId();
        Integer[] checkItemIds = checkGroupDTO.getCheckitemIds();
        if (checkItemIds!=null && checkItemIds.length!=0){

            for (Integer checkItemId : checkItemIds) {
                baseMapper.addCheckGroupIdAndChitemId(checkGroupId, checkItemId);
            }

        }
        return flag;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        QueryWrapper<CheckGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        Page<CheckGroup> page = null;
        if (StringUtils.isBlank(queryPageBean.getQueryString())){
            page = this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }else {
            wrapper.and(i->i.like("code",queryPageBean.getQueryString()));
            wrapper.or().like("name",queryPageBean.getQueryString());
            wrapper.or().like("helpcode",queryPageBean.getQueryString());
            page = this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }

        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public CheckGroupVO findCheckItemInfoByGroupId(int id) {
        List<CheckItem> checkItemList = checkItemService.list();
        int[] checkItemIds = baseMapper.findCheckItemIdsByGroupId(id);
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItemList(checkItemList);
        checkGroupVO.setCheckItemIds(checkItemIds);
        return checkGroupVO;
    }

    @Override
    public List<CheckItem> findCheckItemByGid(Integer id) {
        return null;
    }

    @Override
    public boolean deleteCheckGroup(int id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setIs_delete(1);
        return this.updateById(checkGroup);
    }

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper<CheckGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        return this.list(wrapper);
    }
}
